package com.ruoyi.web.controller.app;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.StatusLog;
import com.ruoyi.system.service.IStatusLogService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ReportInfo;
import com.ruoyi.system.service.IReportInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 登记信息Controller
 *
 * @author ruoyi
 * @date 2022-03-19
 */
@RestController
@RequestMapping("/system/info/report")
public class ReportInfoController extends BaseController {
    @Autowired
    private IReportInfoService reportInfoService;

    @Autowired
    AppController appController;

    @Autowired
    IStatusLogService statusLogService;

    /**
     * 查询登记信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ReportInfo reportInfo) {
        startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<ReportInfo> list = reportInfoService.selectReportInfoList(reportInfo);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSerialNumber(String.valueOf(((pageNum - 1) * pageSize) + (i + 1)));
        }
        return getDataTable(list);
    }

    /**
     * 查询登记信息列表
     */
    @GetMapping("/orgReportlist")
    public TableDataInfo orgReportlist(ReportInfo reportInfo) {
        startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        List<ReportInfo> list = reportInfoService.selectOrgReportInfoList(reportInfo,user.getDeptId());
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSerialNumber(String.valueOf(((pageNum - 1) * pageSize) + (i + 1)));
        }
        return getDataTable(list);
    }




    /**
     * 无分页列表
     */
    @GetMapping("/getList")
    public TableDataInfo getList(ReportInfo reportInfo) {
        startPage();
        List<ReportInfo> list = reportInfoService.selectReportInfoList(reportInfo);
        return getDataTable(list);
    }

    /**
     * 导出登记信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "登记信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportInfo reportInfo) {
        List<ReportInfo> list = reportInfoService.selectReportInfoList(reportInfo);
        ExcelUtil<ReportInfo> util = new ExcelUtil<ReportInfo>(ReportInfo.class);
        util.exportExcel(response, list, "登记信息数据");
    }

    /**
     * 获取登记信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(reportInfoService.selectReportInfoById(id));
    }

    /**
     * 新增登记信息
     */
    //@PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "登记信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportInfo reportInfo) {
        return toAjax(reportInfoService.insertReportInfo(reportInfo));
    }

    /**
     * 修改登记信息
     */
    //@PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "登记信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportInfo reportInfo) {
        if(Objects.equals(reportInfo.getState(),"3")){
            // 暂时停用
            appController.removeyuyue(reportInfo);
        }
        ReportInfo reportInfo1 = reportInfoService.selectReportInfoById(reportInfo.getId());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        StatusLog statusLog = new StatusLog();
        statusLog.setId(IdUtil.simpleUUID());
        statusLog.setReasonDeactivation(reportInfo1.getReasonDeactivation());
        statusLog.setOperationId(user.getUserId().toString());
        statusLog.setReportId(reportInfo1.getId());
        statusLog.setCreateTime(new Date());
        statusLog.setOperation(reportInfo1.getState());
        statusLogService.insertStatusLog(statusLog);


        return toAjax(reportInfoService.updateReportInfo(reportInfo));
    }

    /**
     * 删除登记信息
     */
    //@PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "登记信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(reportInfoService.deleteReportInfoByIds(ids));
    }
}
